﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Linq;

namespace XcoAppSpaces.Transport.Sockets
{
	static class ArrayCopyHelper
	{
		public static T[] Concat<T>(this T[] left, T[] right)
		{
			return ConcatArrays(left, right);
		}

		public static T[] ConcatArrays<T>(params T[][] list)
		{
			var result = new T[list.Sum(a => a.Length)];
			int offset = 0;
			for (int x = 0; x < list.Length; x++)
			{
				list[x].CopyTo(result, offset);
				offset += list[x].Length;
			}
			return result;
		}

		public static T[] SubArray<T>(this T[] source, int offset)
		{
			return source.SubArray(offset, source.Length - offset);
		}

		public static T[] SubArray<T>(this T[] source, int offset, int length)
		{
			var sub = new T[length];
			Array.Copy(source, offset, sub, 0, length);
			return sub;
		}
	}
}
